using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.CommonWeb;
using System.IO;

public partial class admin_webparts_Plug_ins_Files_DeleteItem : System.Web.UI.UserControl
{
    public static string ROOT_PATH = "../" + Constant.UPLOAD_FILE_PATH + "Files/";
    public static string FILES_MESSAGE = "FILES_MESSAGE";
    public static string FILES_MESSAGE_TYPE = "FILES_MESSAGE_TYPE";

    public string _Message = string.Empty;
    public string _CurrentPath = "/";
    public string _CurrentItem = "";
    public int _ActionType = 1; // 1: dir, 2: file

    protected void Page_Load(object sender, EventArgs e)
    {
        btnSave1.Text = LanguageUtility.GetLanguageText("DELETE", Session);

        try
        {
            string action = Request.QueryString["action"];

            if (Request.QueryString["file"] != null)
            {
                _CurrentPath = Request.QueryString["file"];
            }

            string[] paths = _CurrentPath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
            _CurrentItem = paths[paths.Length - 1];

            if ("deletedir".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                _ActionType = 1;
            }
            else
            {
                _ActionType = 2;
            }

            string currentServerPath = Path.Combine(Server.MapPath(ROOT_PATH), this._CurrentPath.TrimStart('/').Replace("/", "\\"));
            if ((_ActionType == 1 && !Directory.Exists(currentServerPath))
                || (_ActionType == 2 && !File.Exists(currentServerPath)))
            {
                Response.Redirect("?type=page&name=files");
                return;
            }
        }
        catch (Exception)
        {
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string currentServerPath = Path.Combine(Server.MapPath(ROOT_PATH), this._CurrentPath.Replace("/", "\\").Substring(1));

            this.SetCookie(FILES_MESSAGE_TYPE, "0");
            if (_ActionType == 1)
            {
                Directory.Delete(currentServerPath, true);
                this.SetCookie(FILES_MESSAGE, LanguageUtility.GetLanguageText("DIR_DELETED", Session));
            }
            else
            {
                File.Delete(currentServerPath);
                this.SetCookie(FILES_MESSAGE, LanguageUtility.GetLanguageText("FILE_DELETED", Session));
            }

            Response.Redirect("?type=page&name=files&file=" + Server.UrlEncode(this.ParentPath), false);
        }
        catch (Exception ex)
        {
            this._Message = ex.Message;
        }
    }

    public void SetCookie(string name, string value)
    {
        //CookieUtility.SetCookie(name, value, DateTime.Now.AddDays(1), Request, Response);
        Session[name] = value;
    }

    public string ParentPath
    {
        get
        {
            if (_CurrentPath == "/")
            {
                return null;
            }

            string parentPath = _CurrentPath.Substring(0, _CurrentPath.Length - 1);
            parentPath = parentPath.Substring(0, parentPath.LastIndexOf("/") + 1);

            return parentPath;
        }
    }
}
